﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EntModApp.EntServices.Presentation
{
  public enum MessageBoxAction
  {
    Confirm,
    Reject,
    Cancel
  }

  public enum MessageBoxIntention
  {
    None,
    Error,
    Question,
    Warning,
    Information,
  }

  public interface IMessageBoxService
  {
    MessageBoxAction Show(
      string message,
      string caption = null,
      bool allowCancel = false,
      bool allowReject = false,
      MessageBoxIntention intention = MessageBoxIntention.None,
      MessageBoxAction defaultAction = MessageBoxAction.Confirm
      );
  }

  public static class MessageBoxServiceExtensions
  {
    public static bool Alert(this IMessageBoxService service, string message, string caption = null, MessageBoxIntention intention = MessageBoxIntention.None)
    {
      if (service == null) throw new ArgumentNullException("service");
      return service.Show(message, caption, false, false, intention, MessageBoxAction.Confirm) == MessageBoxAction.Confirm;
    }

    public static bool AlertOrCancel(this IMessageBoxService service, string message, string caption, MessageBoxIntention intention = MessageBoxIntention.None, bool cancelByDefault = false)
    {
      if (service == null) throw new ArgumentNullException("service");
      return service.Show(message, caption, true, false, intention, cancelByDefault ? MessageBoxAction.Cancel : MessageBoxAction.Confirm) == MessageBoxAction.Confirm;
    }

    public static bool Confirm(this IMessageBoxService service, string message, string caption = null, MessageBoxIntention intention = MessageBoxIntention.None, bool rejectByDefault = false)
    {
      if (service == null) throw new ArgumentNullException("service");
      return service.Show(message, caption, false, true, intention, rejectByDefault ? MessageBoxAction.Reject : MessageBoxAction.Confirm) == MessageBoxAction.Confirm;
    }

    public static bool? ConfirmOrCancel(this IMessageBoxService service, string message, string caption = null, MessageBoxIntention intention = MessageBoxIntention.None, MessageBoxAction defaultAction = MessageBoxAction.Confirm)
    {
      if (service == null) throw new ArgumentNullException("service");
      var result = service.Show(message, caption, true, true, intention, defaultAction);
      if (result == MessageBoxAction.Confirm) return true;
      if (result == MessageBoxAction.Reject) return false;
      return null;
    }
  }
}
